import SyntaxHighlighter from '@/components/SyntaxHighlighter'
import { useEffect } from 'react'

const code1 = `
  const nums = [2, 11, 15, 7, 4, 10, 5];
  const target = 9;
`

const code2 = `
  const setFn = () => {
    const nums = [2, 11, 15, 7, 4, 10, 5]
    const target = 9
    let findValue = []

    const cloneArr = JSON.parse(JSON.stringify(nums))

    for (let i = 0; i < nums.length; i++) {
      const arr = [
        { [i]: nums[i] }
      ]
      for (let j = 0; j < cloneArr.length; j++) {
        if (nums[i] + cloneArr[j] === target) {
          const findIndex = nums.indexOf(cloneArr[j])
          arr.push({ [findIndex]: cloneArr[j] })
          findValue.push(arr)
          cloneArr.splice(i, 1)
          cloneArr.splice(j, 1)
        }
      }
    }

    console.log('findValue', findValue)
  }
`

function FindTarget() {

  const setFn = () => {
    const nums = [2, 11, 15, 7, 4, 10, 5]
    const target = 9
    let findValue = []

    const cloneArr = JSON.parse(JSON.stringify(nums))

    for (let i = 0; i < nums.length; i++) {
      const arr = [
        { [i]: nums[i] }
      ]
      for (let j = 0; j < cloneArr.length; j++) {
        if (nums[i] + cloneArr[j] === target) {
          const findIndex = nums.indexOf(cloneArr[j])
          arr.push({ [findIndex]: cloneArr[j] })
          findValue.push(arr)
          cloneArr.splice(i, 1)
          cloneArr.splice(j, 1)
        }
      }
    }

    console.log('findValue', findValue)
  }

  useEffect(() => {
    setFn()
  }, [])
  
  return (
    <div>
      <p>两数之和给定一个数组 nums 和一个目标值 target，在数组中找出和为目标值的两个数，并且返回下标。</p>
      <SyntaxHighlighter children={code1} />
      <p>代码实现逻辑：</p>
      <SyntaxHighlighter children={code2} />
    </div>
  )
}

export default FindTarget